package com.jianzhile.jobs.activities;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.base.MyApplication;
import com.jianzhile.jobs.utils.CommonUtil;
import com.jianzhile.jobs.utils.HttpUtil;
import com.jianzhile.jobs.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TestActivity extends AppCompatActivity {

    @Bind(R.id.activity_test_api_et_address)
    public EditText apiEt;

    @Bind(R.id.checkBox1)
    public CheckBox ifUUidCB;
    @Bind(R.id.et_params_name_1)
    public EditText paramName1;
    @Bind(R.id.et_params_value_1)
    public EditText paramValue1;
    @Bind(R.id.et_params_name_2)
    public EditText paramName2;
    @Bind(R.id.et_params_value_2)
    public EditText paramValue2;
    @Bind(R.id.et_params_name_3)
    public EditText paramName3;
    @Bind(R.id.et_params_value_3)
    public EditText paramValue3;
    @Bind(R.id.et_params_name_4)
    public EditText paramName4;
    @Bind(R.id.et_params_value_4)
    public EditText paramValue4;
    @Bind(R.id.et_params_name_5)
    public EditText paramName5;
    @Bind(R.id.et_params_value_5)
    public EditText paramValue5;
    @Bind(R.id.et_params_name_6)
    public EditText paramName6;
    @Bind(R.id.et_params_value_6)
    public EditText paramValue6;
    @Bind(R.id.et_params_name_7)
    public EditText paramName7;
    @Bind(R.id.et_params_value_7)
    public EditText paramValue7;
    @Bind(R.id.et_params_name_8)
    public EditText paramName8;
    @Bind(R.id.et_params_value_8)
    public EditText paramValue8;
    @Bind(R.id.et_params_name_9)
    public EditText paramName9;
    @Bind(R.id.et_params_value_9)
    public EditText paramValue9;
    @Bind(R.id.et_params_name_10)
    public EditText paramName10;
    @Bind(R.id.et_params_value_10)
    public EditText paramValue10;
    @Bind(R.id.et_params_name_11)
    public EditText paramName11;
    @Bind(R.id.et_params_value_11)
    public EditText paramValue11;

    @Bind(R.id.test_api_answer)
    public TextView valueTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData(apiEt.getText().toString().trim());
            }
        });
    }


    private void loadData(String api) {
        // 封装请求数据信息
        Map<String, Object> params = new HashMap<String, Object>();
        String p1n = paramName1.getText().toString().trim();
        String p1v = paramValue1.getText().toString().trim();
        String p2n = paramName2.getText().toString().trim();
        String p2v = paramValue2.getText().toString().trim();
        String p3n = paramName3.getText().toString().trim();
        String p3v = paramValue3.getText().toString().trim();
        String p4n = paramName3.getText().toString().trim();
        String p4v = paramValue3.getText().toString().trim();

        String p5n = paramName1.getText().toString().trim();
        String p5v = paramValue1.getText().toString().trim();
        String p6n = paramName2.getText().toString().trim();
        String p6v = paramValue2.getText().toString().trim();
        String p7n = paramName3.getText().toString().trim();
        String p7v = paramValue3.getText().toString().trim();
        String p8n = paramName3.getText().toString().trim();
        String p8v = paramValue3.getText().toString().trim();

        String p9n = paramName1.getText().toString().trim();
        String p9v = paramValue1.getText().toString().trim();
        String p10n = paramName2.getText().toString().trim();
        String p10v = paramValue2.getText().toString().trim();
        String p11n = paramName3.getText().toString().trim();
        String p11v = paramValue3.getText().toString().trim();


        if (ifUUidCB.isChecked()) {
            params.put("uuid", MyApplication.getInstance().mUser.uid);
        }

        if (!p1n.equals("")) {
            params.put(p1n, p1v);
        }
        if (!p2n.equals("")) {
            params.put(p2n, p2v);
        }
        if (!p3n.equals("")) {
            params.put(p3n, p3v);
        }
        if (!p4n.equals("")) {
            params.put(p4n, p4v);
        }

        if (!p5n.equals("")) {
            params.put(p5n, p5v);
        }
        if (!p6n.equals("")) {
            params.put(p6n, p6v);
        }
        if (!p7n.equals("")) {
            params.put(p7n, p7v);
        }
        if (!p8n.equals("")) {
            params.put(p8n, p8v);
        }

        if (!p9n.equals("")) {
            params.put(p9n, p9v);
        }
        if (!p10n.equals("")) {
            params.put(p10n, p10v);
        }
        if (!p11n.equals("")) {
            params.put(p11n, p11v);
        }

        if (apiEt.getText().equals("Car/Take") && params.size() == 0){
            ToastUtils.showBottomToast(this, "啥玩意没填你点我干啥");
        }

        HttpUtil.getInstance().requestWithSecurity(this, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                valueTv.setText(response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.getInt("code");
                    if (code == 1) {
                        Gson gson = new Gson();
                        JSONArray jsonArray = jsonObject.getJSONArray("data");

                    }else{
                        valueTv.setText("不管返回的什么，反正返回的code不等于1");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    valueTv.setText("谁写的接口啊，都特么错误了");
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }, CommonUtil.getInstance().getParams(params), api, false);
    }



}
